package cn.edu.zjut.controller;

import cn.edu.zjut.base.BaseController;
import cn.edu.zjut.entity.UserEntity;
import cn.edu.zjut.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @description:
 * @author:ZhouDan
 * @date: 2020/5/25
 * @version: 1.0
 */
@Slf4j
@SessionAttributes({"authorizationRequest"})
@RestController
public class LoginController extends BaseController {
    private IUserService userService;
    private AuthenticationManager authenticationManager;
    private SecurityProperties securityProperties;

    String tokenHead = "Bearer ";
    String tokenHeader = "Authorization";
    private UserDetailsService userDetailsService;

    @Autowired
    public LoginController(IUserService userService, AuthenticationManager authenticationManager, SecurityProperties securityProperties, UserDetailsService userDetailsService) {
        this.userService = userService;
        this.authenticationManager = authenticationManager;
        this.securityProperties = securityProperties;
        this.userDetailsService = userDetailsService;
    }



    @PostMapping("/login")
    public Object refreshAndGetAuthenticationToken (@RequestParam("userName") String userName, @RequestParam("password") String password) {
        UserEntity user = userService.login(userName,password);
        if(user == null){
            return ResponseEntity.ok(response(1004,null));
        }
        Map<String,String> userMap = new HashMap<>();
        userMap.put("token", UUID.randomUUID().toString().replace("-",""));
        userMap.put("user",user.toString());
        return ResponseEntity.ok(response(200,userMap));
    }

    @PostMapping("/update")
    public Object updatePassword(@RequestParam String userName,@RequestParam String oldPassword,@RequestParam String newPassword, @RequestParam String rePassword){
        if(null == userName || null == newPassword || null == rePassword){
            return response(1018,null);
        }
        UserEntity user = userService.getByUserName(userName);
        if(user == null){
            return response(1016,null);
        }
        if(!oldPassword.equals(user.getPassword())){
            return response(1004,null);
        }
        if(!newPassword.equals(rePassword)){
            return response(1005,null);
        }
        try {
            user.setPassword(newPassword);
            return response(200,userService.save(user));
        }catch (Exception e) {
            return response(1019, e);
        }
    }


    @PostMapping("/user/logout")
    public Object logout(HttpServletRequest httpServletRequest){
        log.info(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        HttpSession session = httpServletRequest.getSession();
        // 让以前的session失效
        session.invalidate();
        return response(200,userDetails);
    }
}